package com.hxy.oss.util;

import com.hxy.oss.entity.FileInfo;
import io.minio.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author haoxuyang
 * @Date 2024/9/18 10:15
 */
@Component
public class MinioUtil {

    @Resource
    private MinioClient minioClient;

    /**
     * 创建bucket桶
     *
     * @param bucket
     * @throws Exception
     */
    public void creatBucket(String bucket) throws Exception {
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (!exists) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
        }
    }

    /**
     * 上传文件
     *
     * @param inputStream
     * @param bucket
     * @param fileName
     * @throws Exception
     */
    public void updateFile(InputStream inputStream, String bucket, String fileName) throws Exception {
        minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(fileName).stream(inputStream, -1, Integer.MIN_VALUE).build());

    }

    /**
     * 获取所有桶名
     *
     * @throws Exception
     */
    public List<String> getAllBucket() throws Exception {
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * 获取所有文件
     *
     * @throws Exception
     */
    public List<FileInfo> getAllFile(String bucket) throws Exception {
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucket).build());
        List<FileInfo> fileInfoList = new LinkedList<>();

        for (Result<Item> result : results) {
            FileInfo fileInfo = new FileInfo();
            Item item = result.get();
            fileInfo.setFileName(item.objectName());
            fileInfo.setDirectoryFile(item.isDir());
            fileInfo.setEtag(item.etag());
            fileInfoList.add(fileInfo);
        }
        return fileInfoList;
    }

    /**
     * 下载文件
     *
     * @param bucket
     * @param fileName
     * @throws Exception
     */
    public InputStream downLoadFile(String bucket, String fileName) throws Exception {
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(fileName).build());
    }

    /**
     * 删除桶
     *
     * @param bucket
     * @throws Exception
     */
    public void delFile(String bucket) throws Exception {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
    }

    /**
     * 删除文件
     *
     * @param bucket
     * @param fileName
     * @throws Exception
     */
    public void delFile(String bucket, String fileName) throws Exception {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(fileName).build());
    }
}
